/*
 * BasicFileFilter.java
 *
 * Created on September 15, 2007, 3:45 PM
 */

package picto.tools;

import java.io.File;
import java.text.MessageFormat;
import javax.swing.filechooser.FileFilter;

/**
 * A simple file filter for JFileChoosers. It's 
 * often good practice for subclasses to define
 * a static final String variable, EXT, for the 
 * constant extension.
 * 
 * @author davedes
 */
public class BasicFileFilter extends AbstractFileFilter {
    
    /**
     * The extension of this filter.
     */
    private String ext;
    
    /**
     * Creates a new instance of BasicFileFilter.
     * @param description The message, such as "PNG Files (.png)"
     * @param ext The extension, such as ".png"
     */
    public BasicFileFilter(String description, String ext) {
        super(description);
        this.ext = ext;
    }
    
    /**
     * Whether to accept the file for this filter.
     * @param f the file
     * @return true if we are accepting it
     */
    public boolean accept(File f) {
        if (f!=null) {
            if (f.isDirectory())
                return true;
            return f.getName().toLowerCase().endsWith(ext.toLowerCase());
        } else
            return false;
    }
    
    /**
     * Gets the extension of this filter.
     * @return the extension
     */
    public String getExtension() {
        return ext;
    }
}

